package com.zlx.xbhy.controller;

import com.github.pagehelper.PageInfo;
import com.zlx.xbhy.entity.Meeting;
import com.zlx.xbhy.entity.MeetingJoin;
import com.zlx.xbhy.entity.Result;
import com.zlx.xbhy.entity.User;
import com.zlx.xbhy.service.MeetingJoinService;
import com.zlx.xbhy.service.MeetingService;
import com.zlx.xbhy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName MeetingController
 * @Description TODO
 * @Author Edward
 * @Date 2020/11/22 15:37
 */
@RestController
@RequestMapping("/meeting")
public class MeetingController {
    @Autowired
    private MeetingService service;
    @Autowired
    private MeetingJoinService meetingJoinService;
    @Autowired
    private UserService userService;

    @GetMapping("/selectPage/{start}/{pageSize}")
    public Result selectPage(@RequestParam("title") String title, @RequestParam("status") Integer status, @PathVariable("start") Integer start, @PathVariable("pageSize") Integer pageSize) {
        PageInfo<Meeting> meetingPageInfo = service.selectPage(title, status, start, pageSize);
        return new Result(meetingPageInfo);
    }

    //获取会议需到人数
    @GetMapping("/needJoinCount/{mid}")
    public Result getNeedJoinCount(@PathVariable Long mid) {
        Meeting meeting = service.selectByPrimaryKey(mid);
        //需要参加会议的人数
        int length = meeting.getMakeUser().split(",").length;
        return new Result(length);
    }

    //获取已经参加会议的人数
    @GetMapping("joinedMeetingCount/{mid}")
    public Result joinedMeetingCount(@PathVariable Long mid) {
        MeetingJoin meetingJoin = new MeetingJoin();
        meetingJoin.setmId(mid);
        return new Result(meetingJoinService.selectCount(meetingJoin));
    }

    //判断用户是否参与了会议
    @GetMapping("isJoinMeeting/{uid}/{mid}")
    public Result isJoinMeeting(@PathVariable Long uid, @PathVariable Long mid) {
        MeetingJoin meetingJoin = new MeetingJoin();
        meetingJoin.setmId(mid);
        meetingJoin.setuId(uid);
        return new Result(meetingJoinService.selectCount(meetingJoin));
    }

    //参与会议
    @PostMapping("joinMeeting/{uid}/{mid}")
    public Result joinMeeting(@PathVariable Long uid, @PathVariable Long mid) {
        MeetingJoin meetingJoin = new MeetingJoin();
        meetingJoin.setmId(mid);
        meetingJoin.setuId(uid);
        return new Result(meetingJoinService.insert(meetingJoin));
    }

    //取消参与会议
    @PutMapping("cancelMeeting/{uid}/{mid}")
    public Result cancelMeeting(@PathVariable Long uid, @PathVariable Long mid) {
        MeetingJoin meetingJoin = new MeetingJoin();
        meetingJoin.setmId(mid);
        meetingJoin.setuId(uid);
        return new Result(meetingJoinService.delete(meetingJoin));
    }

    //根据部门获取用户
    @GetMapping("getUsers/{deptId}")
    public Result getUsers(@PathVariable Long deptId){
        User user = new User();
        user.setDeptId(deptId);
        return new Result(userService.select(user));
    }
    //添加会议
    @PostMapping("addMeeting/{ids}")
    public Result addMeeting(@RequestBody Meeting meeting,@PathVariable String ids){
        ids=ids.replace("+''","");
        System.out.println(meeting);
        System.out.println(ids);
        service.addMeeting(meeting,ids);
        return new Result("添加成功！");
    }
}
